﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class ChiTietPhieuGuiHangDAO
    {
        public List<ChiTietPhieuGuiHangDTO> HienThi(string MAPGH)
        {
            string sql = "select MACTPGH, MAMH, SOLUONG, GHICHU from CTPHIEUGUIHANG where MAPGH=";
            sql += "'" + MAPGH + "'";
            SqlConnection conn = DataProvider.ConnectionDB();
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataReader reader = command.ExecuteReader();

            List<ChiTietPhieuGuiHangDTO> danhsach = new List<ChiTietPhieuGuiHangDTO>();
            while (reader.Read())
            {
                ChiTietPhieuGuiHangDTO dto = new ChiTietPhieuGuiHangDTO();
                dto.MACTPGH = reader.GetString(0);
                dto.MAMH = reader.GetString(1);
                dto.SOLUONG = reader.GetInt32(2);
                dto.GHICHU = reader.GetString(3);
                danhsach.Add(dto);
            }
            conn.Close();
            return danhsach;
        }

        public int ThemCTPGH(ChiTietPhieuGuiHangDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "insert into CTPHIEUGUIHANG values(";
            sql += "N'" + dto.MACTPGH + "',";
            sql += "N'" + dto.MAPGH + "',";
            sql += "N'" + dto.MAMH + "',";
            sql += "'" + dto.SOLUONG + "',";
            sql += "N'"+dto.GHICHU+"'";
            sql += ")";

            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public int CapNhatCTPGH(ChiTietPhieuGuiHangDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "update CTPHIEUGUIHANG";
            sql += " set MACTPGH=";
            sql += "'" + dto.MACTPGH + "',";
            sql += " MAPGH=";
            sql += "'" + dto.MAPGH + "',";
            sql += " MAMH=";
            sql += "N'" + dto.MAMH + "',";
            sql += " SOLUONG=";
            sql += dto.SOLUONG + ",";
            sql += " GHICHU=";
            sql += "N'" + dto.GHICHU + "'";
            sql += " where MACTPGH=";
            sql += "'" + dto.MACTPGH + "'";

            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public int KiemTraCTPGH(string mactpgh)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "Select count(*) from CTPHIEUGUIHANG where MACTPGH=";
            sql += "'" + mactpgh + "'";
            SqlCommand command = new SqlCommand(sql, conn);
            int kq = (int)command.ExecuteScalar();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }
    }
}
